package com.fz.service.merchant.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.common.ResponseEntity;
import com.fz.dto.merchant.merRole.MerRoleAdd;
import com.fz.dto.merchant.merRole.MerRoleMenuDto;
import com.fz.dto.merchant.merRole.MerRoleQuery;
import com.fz.dto.merchant.merRole.MerRoleUpd;
import com.fz.entity.merchant.MerRole;
import com.fz.entity.merchant.MerRoleMenu;
import com.fz.mapper.merchant.MerRoleMapper;
import com.fz.service.merchant.MerMenuService;
import com.fz.service.merchant.MerRoleService;
import com.fz.utils.StringUtils;
import com.fz.vo.merchant.merMenu.MerMenuTreeVo;
import com.fz.vo.merchant.merMenu.MerMenuVo;
import com.fz.vo.merchant.merMenu.MerRoleMenuTreeVo;
import com.fz.vo.merchant.merRole.MerRoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@SuppressWarnings("unchecked")
public class MerRoleServiceImpl implements MerRoleService {

    @Resource
    private MerRoleMapper merRoleMapper;

    @Resource
    private MerMenuService merMenuService;

    /**
     * @方法描述: 角色管理查询角色，以及菜单
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<MerRoleMenuTreeVo> findMerRoleList(int pageNum, int pageSize, MerRoleQuery merRoleQuery) throws Exception{
        MerMenuTreeVo menuTreeDtoList = merMenuService.findMerMenuTree();
        Page<MerRoleVo> page = new Page<>(pageNum,pageSize);
        IPage<MerRoleVo> roleVos = merRoleMapper.findMerRoleList(page);
        MerRoleMenuTreeVo roleMenuTreeVo = new MerRoleMenuTreeVo();
        roleMenuTreeVo.setMerMenuTreeVo(menuTreeDtoList);
        roleMenuTreeVo.setRoleVos(roleVos);
        return ResponseEntity.buildSuccess(roleMenuTreeVo);
    }

    /**
     * @方法描述: 前端给用户角色id，获取到之后用来查询拥有的目录
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<List<MerMenuVo>> findMerMenuById(int roleId) throws Exception{
        List<MerMenuVo> roleOfMenu =  merRoleMapper.findMerMenuByRoleId(roleId);
        return ResponseEntity.buildSuccess(roleOfMenu);
    }

    /**
     * @方法描述: 模糊查询拥有的目录
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity<List<MerRoleVo>> findMerRoleSelect(MerRoleQuery merRoleQuery) throws Exception{
        List<MerRoleVo> roleVos = merRoleMapper.findMerRoleVo(merRoleQuery);
        return ResponseEntity.buildSuccess(roleVos);
    }

    /**
     * @方法描述: 新增角色
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity addMerRole(MerRoleAdd merRoleAdd) throws Exception{
        MerRole role = new MerRole();
        role.setName(merRoleAdd.getName());
        role.setDataScope(merRoleAdd.getDataScope());
        role.setLevel(merRoleAdd.getLevel());
        role.setPermission(merRoleAdd.getPermission());
        role.setRemark(merRoleAdd.getRemark());
        merRoleMapper.insert(role);
        return ResponseEntity.buildSuccess();
    }

    /**
     * @方法描述: 根据修改的角色属性，修改数据库
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    public ResponseEntity updateMerRole(MerRoleUpd merRoleUpd) throws Exception{
        MerRole role = new MerRole();
        role.setId(merRoleUpd.getId());
        role.setName(merRoleUpd.getName());
        role.setPermission(merRoleUpd.getPermission());
        role.setDataScope(merRoleUpd.getDataScope());
        role.setLevel(merRoleUpd.getLevel());
        role.setRemark(merRoleUpd.getRemark());
        merRoleMapper.updateById(role);
        return ResponseEntity.buildSuccess(role);
    }

    /**
     * @方法描述: 建立权限，角色的全新对应关系
     * @参数:
     * @返回值:
     * @创建人:
     * @创建时间:: 2021/3/22
     */
    @Override
    @Transactional
    public ResponseEntity saveMerRoleMenu(MerRoleMenuDto merRoleMenuDto) throws Exception{
        MerRoleMenu roleMenu = new MerRoleMenu();
        roleMenu.setRoleId(merRoleMenuDto.getRoleid());
        merRoleMapper.delMerRoleMenu(roleMenu);
        merRoleMapper.insertMerRoleMenu(StringUtils.strToList(merRoleMenuDto.getMenuids()), merRoleMenuDto.getRoleid());
        return ResponseEntity.buildSuccess();
    }



}
